/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sv.com.intesal.compra.managed;

import java.util.List;
import javax.faces.bean.ManagedBean;
import sv.com.intesal.clases.abstracts.ViewScopedAbstract;
import sv.com.intesal.clases.others.FormularioName.COMPRA.Mtto;
import sv.com.intesal.compra.bean.Linea;
import sv.com.intesal.compra.bean.SubLinea;
import sv.com.intesal.compra.controlador.SubLineaControlador;

/**
 *
 * @author ejerez
 */
@ManagedBean(name = "subLineaManaged")
public class SubLineaManaged extends ViewScopedAbstract {

    private SubLinea selectedSubLinea;
    private List<SubLinea> subLineas;
    private List<Linea> lineas;

    public SubLineaManaged() {
        this.selectedSubLinea = new SubLinea();
        this.lineas = null;
        this.subLineas = null;
        this.comboBox();
    }

    private void comboBox() {
        try {
            if (this.lineas == null) {
                SubLineaControlador cc = new SubLineaControlador();
                this.lineas = cc.getLineasActivos();
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.messageError(m.getIerror());
        }
    }

    public void fillGrid() {
        try {
            if (this.subLineas == null) {
                if (sm.validar_rol_usuario(Mtto.wfrmSubLinea, sm.buscar) == true) {
                    SubLineaControlador cc = new SubLineaControlador();
                    this.subLineas = cc.getSubLineas();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.messageError(m.getIerror());
        }
    }

    public void completeTransaction() {
        try {
            if (this.validar() == true) {
                if (this.selectedSubLinea.getCodigo_sl() == null) {
                    if (sm.validar_rol_usuario(Mtto.wfrmSubLinea, sm.agregar) == true) {
                        SubLineaControlador cc = new SubLineaControlador();
                        cc.insert(this.selectedSubLinea);
                        m.messageInfo(m.getIinformation());
                        lm.limpiar();
                    }
                } else {
                    if (sm.validar_rol_usuario(Mtto.wfrmSubLinea, sm.modificar) == true) {
                        SubLineaControlador cc = new SubLineaControlador();
                        cc.update(this.selectedSubLinea);
                        m.messageInfo(m.getImodificada());
                        lm.limpiar();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.messageError(m.getIerror());
        }
    }

    private Boolean validar() throws Exception {
        try {
            Boolean var_return = true;
            if (this.selectedSubLinea.getCodigo_l() == null) {
                var_return = false;
                m.messageAdvertencia(m.campoError("LINEA"));
            }
            if (this.selectedSubLinea.getNombre_sl().equals("")) {
                var_return = false;
                m.messageAdvertencia(m.campoError("NOMBRE"));
            }
            return var_return;
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    /*SETTERS AND GETTERS*/
    public List<Linea> getLineas() {
        return lineas;
    }

    public void setLineas(List<Linea> lineas) {
        this.lineas = lineas;
    }

    public SubLinea getSelectedSubLinea() {
        return selectedSubLinea;
    }

    public void setSelectedSubLinea(SubLinea selectedSubLinea) {
        this.selectedSubLinea = selectedSubLinea;
    }

    public List<SubLinea> getSubLineas() {
        return subLineas;
    }

    public void setSubLineas(List<SubLinea> subLineas) {
        this.subLineas = subLineas;
    }
}
